package com.zbkj.crmeb.merchant.admin.controller;

import com.common.PageParamRequest;
import com.constants.Constants;
import com.utils.ExcelUtil;
import com.utils.PriceUtil;
import com.zbkj.crmeb.enums.ProductTypeEnum;
import com.zbkj.crmeb.export.vo.MerFlowExcelVo;
import com.zbkj.crmeb.export.vo.ProductExcelVo;
import com.zbkj.crmeb.export.vo.StoreOrderExcelVo;
import com.zbkj.crmeb.merchant.admin.service.*;
import com.zbkj.crmeb.store.model.StoreOrder;
import com.zbkj.crmeb.store.request.StoreOrderSearchRequest;
import com.zbkj.crmeb.store.request.StoreProductSearchRequest;
import com.zbkj.crmeb.system.service.SystemConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;


/**
 * 服务商导出
 */
@Slf4j
@Controller
@RequestMapping("api/admin/mer/agent/export/excel")
@Api(tags = "服务商导出 -- Excel")
public class MerAgentExcelController extends AdminBaseController {

    @Autowired
    PriceUtil priceUtil;
    @Autowired
    private MerExcelService excelService;
    @Autowired
    SystemConfigService systemConfigService;
    @Autowired
    private MerAgentStoreProductService agentStoreProductService;
    @Autowired
    private MerAgentStoreOrderService agentStoreOrderService;

    AtomicInteger atom = new AtomicInteger(0);
    final int max = 10;

    /**
     * 商品导出
     *
     * @param request 搜索条件
     */
    @ApiOperation(value = "云仓商品")
    @RequestMapping(value = "/product", method = RequestMethod.GET)
    public void export(@Validated StoreProductSearchRequest request, HttpServletResponse response) {
        try {
            // 保护一下 最多允许10个人同时导出
            if (atom.getAndIncrement() < max) {
                PageParamRequest pageParamRequest = new PageParamRequest();
                pageParamRequest.setPage(Constants.DEFAULT_PAGE);
                pageParamRequest.setLimit(Constants.EXPORT_MAX_LIMIT);
                //云仓商品
                List<Integer> isNotEurope = Arrays.asList(ProductTypeEnum.INSIDE.getCode());
                request.setIsNotEurope(isNotEurope);
                List<ProductExcelVo> list = agentStoreProductService.getExportList(request, pageParamRequest);
                excelService.parseList(list);
                //sheet名称
                ExcelUtil.setSheetName("store");
                //文件名称前缀
                ExcelUtil.setFileName("产品导出");
                ExcelUtil.writeExcel(response, list, ProductExcelVo.class);
            }
        } finally {
            atom.decrementAndGet();
        }
    }

    @ApiOperation(value = "自建商品")
    @RequestMapping(value = "/inside/product", method = RequestMethod.GET)
    public void exportInsideProductList(@Validated StoreProductSearchRequest request, HttpServletResponse response) {
        try {
            // 保护一下 最多允许10个人同时导出
            if (atom.getAndIncrement() < max) {
                PageParamRequest pageParamRequest = new PageParamRequest();
                pageParamRequest.setPage(Constants.DEFAULT_PAGE);
                pageParamRequest.setLimit(Constants.EXPORT_MAX_LIMIT);
                //自建商品
                request.setIsEurope(ProductTypeEnum.INSIDE.getCode());
                List<ProductExcelVo> list = agentStoreProductService.getExportList(request, pageParamRequest);
                excelService.parseList(list);
                //sheet名称
                ExcelUtil.setSheetName("store");
                //文件名称前缀
                ExcelUtil.setFileName("产品导出");
                ExcelUtil.writeExcel(response, list, ProductExcelVo.class);
            }
        } finally {
            atom.decrementAndGet();
        }
    }


    @ApiOperation(value = "订单导出")
    @RequestMapping(value = "/storeOrder", method = RequestMethod.GET)
    public void storeOrderexport(@Validated StoreOrderSearchRequest request, HttpServletRequest servletRequest, HttpServletResponse response) {
        try {
            // 保护一下 最多允许10个人同时导出
            if (atom.getAndIncrement() < max) {
                List<StoreOrderExcelVo> productExcelVoList = new ArrayList<>();
                if(request.getOrderIds().size() == 0) {
                    PageParamRequest pageParamRequest = new PageParamRequest();
                    pageParamRequest.setPage(Constants.DEFAULT_PAGE);
                    pageParamRequest.setLimit(Constants.EXPORT_MAX_LIMIT);
                    productExcelVoList = agentStoreOrderService.getExportList(request, pageParamRequest);
                }else {
                    // 根据选中的订单id导出
                    List<StoreOrder> list = agentStoreOrderService.lambdaQuery().in(StoreOrder::getOrderId, request.getOrderIds()).list();
                    BeanUtils.copyProperties(list, productExcelVoList);
                }
                //sheet名称
                ExcelUtil.setSheetName("订单信息表");
                //文件名称前缀
                ExcelUtil.setFileName("订单导出");
                ExcelUtil.writeExcelByIsImg(response, productExcelVoList, StoreOrderExcelVo.class, request.getIsImg());
            }
        } finally {
            atom.decrementAndGet();
        }
    }

    /**
     * 服务商流水列表导出
     */
    @ApiOperation(value = "服务商流水列表导出")
    @RequestMapping(value = "/agentFlowExport", method = RequestMethod.GET)
    public void agentFlowExport(@ApiParam(value = "类型:2 可提现余额;3 已冻结金额", required = true) Integer type, PageParamRequest request, HttpServletResponse response) {
        try {
            // 保护一下 最多允许10个人同时导出
            if (atom.getAndIncrement() < max) {
                request.setPage(Constants.DEFAULT_PAGE);
                request.setLimit(Constants.EXPORT_MAX_LIMIT);
                List<MerFlowExcelVo> merFlowList = excelService.getAgentFlowExport(systemAdminService.getAdminAgentId(), type, request);
                //sheet名称
                ExcelUtil.setSheetName("服务商流水信息");
                //文件名称前缀
                ExcelUtil.setFileName("服务商流水");
                ExcelUtil.writeExcel(response, merFlowList, MerFlowExcelVo.class);
            }
        } finally {
            atom.decrementAndGet();
        }
    }

}



